package com.example.tool.task;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName crmQcc
 * @Author 13403
 * @Date 2022/8/918:12
 * @Version 1.0
 **/
public class crmQcc {


    public static void main(String[] args) throws IOException {
        crmQcc crmQcc = new crmQcc();
        String path="C:/Users/13403/Desktop/qcc.xlsx";//获取客户名称的文件
        String filePath = "C:/Users/13403/Desktop/qccdata.xlsx";//写入的文件
        List<Map<String, String>> list= crmQcc.getNameList(path);
        List<Map<String, String>> datalist= new ArrayList<>();

        for (int i = 0; i <list.size(); i++) {
            String name = list.get(i).get("客户名称");
            if (name.length()<=3||name.contains("（")||name.contains("(")){
                continue;
            }
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("当前进行第"+i+"个查询："+name);
            Map<String, String> map = crmQcc.sentQcc(name);
            crmQcc.inserSheetData(filePath,i,map);
        }

        System.out.println("=======end=======");





    }

    public Map<String, String> sentQcc(String name) throws IOException {
        Map<String, String> stringStringHashMap=  new HashMap<>();
        String url=""+name;
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpGet get = new HttpGet(url);
        get.addHeader("Cookie", "");
        HttpResponse response = httpClient.execute(get);
        JSONObject jsonObject = JSONObject.parseObject(EntityUtils.toString(response.getEntity()));
        if (jsonObject.getString("name")!=null){
            stringStringHashMap.put("客户名称",jsonObject.getString("name"));
        }
        if (jsonObject.getString("legalPersonName")!=null){
            stringStringHashMap.put("法定代表人",jsonObject.getString("legalPersonName"));
        }
        if (jsonObject.getString("businessScope")!=null){
            stringStringHashMap.put("经营范围",jsonObject.getString("businessScope"));
        }
        if (jsonObject.getString("property1")!=null){
            stringStringHashMap.put("统一社会信用代码",jsonObject.getString("property1"));
        }
        if(jsonObject.getString("estiblishTime")!=null){
            stringStringHashMap.put("成立日期",jsonObject.getString("estiblishTime"));
        }
        System.out.println(stringStringHashMap.toString());
        return stringStringHashMap;

    }




    /**
     * 从第几行写入数据
     * @param rowNum
     * @param data
     * @throws Exception
     */
    public  void inserSheetData(String filePath,Integer rowNum,Map<String, String> data)  {
        FileInputStream fs=null;
        XSSFWorkbook xssfWorkbook=null;
        FileOutputStream out=null;
        XSSFSheet InsertSheet=null;
        try {

            fs=new FileInputStream(filePath);
            xssfWorkbook = new XSSFWorkbook(fs);
            //获取到工作表，因为一个excel可能有多个工作表
            InsertSheet= xssfWorkbook.getSheetAt(0);
            //向文件中写入数据
            out=new FileOutputStream(filePath);
            //在指定行后追加数据
            XSSFRow row=InsertSheet.getRow(rowNum+1);
            //设置第一个（从0开始）单元格的数据
            row.createCell(11).setCellValue(data.get("客户名称"));
            row.createCell(12).setCellValue(data.get("统一社会信用代码"));
            row.createCell(13).setCellValue(data.get("法定代表人"));
            row.createCell(14).setCellValue(data.get("成立日期"));
            row.createCell(15).setCellValue(data.get("经营范围"));
            out.flush();
            xssfWorkbook.write(out);
            out.close();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (xssfWorkbook!=null){
                try {
                    xssfWorkbook.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fs!=null){
                try {
                    fs.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }




    /**
     * 传入excel,获得公司名称
     * @param path
     * @return
     */
    private  List<Map<String, String>> getNameList(String path){
        File file = new File(path);
        String name = file.getName();
        FileInputStream fileInputStream=null;
        try{
            fileInputStream = new FileInputStream(file);
        }catch (Exception e) {
            e.printStackTrace();
        }
        Map<String, Integer> map=new HashMap();
        map.put("客户名称",0);
        List<Map<String,String>> list = ExcelUtil.parseExcel(fileInputStream, name, 0, 0, 1, map);
        return list;
    }
























}
